<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Records
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: RGBA.php 5 2010-02-14 05:08:39Z indigo.sunrise $
 */

/**
 * Represents an RGBA color record.
 *
 * @package    Pwiff
 * @subpackage Records
 */
class Pwiff_Record_RGBA extends Pwiff_Record_RGB
{
	/**
	 * Alpha value.
	 *
	 * @var int
	 */
	protected $alpha = 255;
	
	/**
	 * Sets the three component colors and alpha.
	 * 
	 * @param  int $red
	 * @param  int $green
	 * @param  int $blue
	 * @param  int $alpha
	 */
	public function setRGBA($red, $green, $blue, $alpha)
	{
		$this->setRGB($red, $green, $blue);
		$this->setAlpha($alpha);
	}
	
	/**
	 * Sets the alpha value.
	 *
	 * @param  int $alpha
	 * @return Pwiff_Record_RGBA
	 */
	public function setAlpha($alpha)
	{
		if ($alpha < 0 OR $alpha > 255) {
			throw new Pwiff_Exception('Alpha value must be between 0 and 255.');
		}
		$this->alpha = $alpha;
		return $this;
	}
	
	/**
	 * Returns the RGBA values as a 4 element array.
	 *
	 * @return array
	 */
	public function getRGBA()
	{
		return array($this->red, $this->green, $this->blue, $this->alpha);
	}
	
	/**
	 * Returns the alpha value.
	 *
	 * @return int
	 */
	public function getAlpha()
	{
		return $this->alpha;
	}
	
	/**
	 * Returns the record length. Always 4.
	 *
	 * @return int
	 */
	public function getRecordLength()
	{
		return 4;
	}
	
	/**
	 * Writes the RGBA values to the given output instance.
	 *
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		parent::write($out);
		$out->writeUI8($this->alpha);
	}
}
